package com.shop.cereshop.app.controller.recommend;

import com.shop.cereshop.app.page.recommend.RecommendCommentVO;
import com.shop.cereshop.app.param.recommend.RecommendCommentAddDTO;
import com.shop.cereshop.app.service.recommend.CereRecommendCommentService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("recommends/comment")
@Slf4j(topic = "RecommendCommentController")
@Api(value = "话题评论模块", tags = "话题评论模块")
public class RecommendCommentController {

    @Resource
    private CereRecommendCommentService cereRecommendCommentService;



    @GetMapping("pages")
    @ApiOperation(value = "获取动态一级评论列表")
    public Result<Page<RecommendCommentVO>> getRecommendCommentPage(
            @RequestParam("page") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer page,
            @RequestParam("pageSize") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer pageSize,
            @RequestParam("recommendId") @NotNull(message = "参数不能为空") @ApiParam(value = "动态ID", required = true) Long recommendId,
            HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        return new Result<>(cereRecommendCommentService.getRecommendCommentPage(page, pageSize, recommendId, null, user), CoReturnFormat.SUCCESS);
    }

    @GetMapping("pages/reply")
    @ApiOperation(value = "获取动态二级评论列表")
    public Result<Page<RecommendCommentVO>> getReplyCommentPage(
            @RequestParam("page") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer page,
            @RequestParam("pageSize") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer pageSize,
            @RequestParam("parentCommentId") @NotNull(message = "参数不能为空") @ApiParam(value = "父级评论id", required = true) Long parentCommentId,
            HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        return new Result<>(cereRecommendCommentService.getRecommendCommentPage(page, pageSize, null, parentCommentId, user), CoReturnFormat.SUCCESS);
    }

    @PostMapping("save")
    @ApiOperation(value = "发布动态评论")
    public Result saveRecommendComment(@RequestBody @NotNull(message = "参数不能为空") RecommendCommentAddDTO param, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        // 获取用户是否可以发布评论
        Integer ifAllowOther = user.getIfAllowOther();
        if (ifAllowOther == 0) {
            return new Result(CoReturnFormat.USER_NOT_ALLOW_COMMENT);
        }

        // 参数校验
        if (param == null) {
            return Result.failed("请求参数不能为空");
        }
        return cereRecommendCommentService.saveRecommendComment(param, user);
    }


    @DeleteMapping("delete")
    @ApiOperation(value = "删除动态评论")
    public Result deleteComment (@RequestParam("recommendCommentId")
                                     @NotNull(message = "参数不能为空")
                                     @ApiParam(value = "动态评论ID", required = true) Long recommendCommentId) throws CoBusinessException {
        cereRecommendCommentService.deleteComment(recommendCommentId);
        return new Result(CoReturnFormat.SUCCESS);
    }

}
